package main

import (
    "../pb"
    "../server"
    "golang.org/x/net/context"
    "sync"
    "github.com/kpango/glg"
    "fmt"
)

type MetaServer struct {
    mu  sync.RWMutex
    sid string
    hex string
    
    leaseManager *server.LeaseManager
    isActive     bool
    stopping     chan interface{}
    stopped      chan interface{}
}

func (ms *MetaServer) RenewLease(ctx context.Context, req *pb.LeaseReq) (*pb.Lease, error) {
    glg.Infof("renew lease req from %s as %v", req.Sid, req.Role)
    return ms.leaseManager.RenewLease(req)
}

func (ms *MetaServer) CheckActive(ctx context.Context, req *pb.Lease) (*pb.ActiveResp, error) {
    glg.Infof("checkalive req from %s, server status is %v", req.Sid, ms.isActive)
    err := ms.leaseManager.Check(req)
    if err != nil {
        glg.Infof("checkalive req lease invalid %v", err)
        return nil, err
    }
    if ms.isActive {
        return &pb.ActiveResp{}, nil
    }
    return nil, fmt.Errorf("server inactive")
}

func (ms *MetaServer) Addr(context.Context, *pb.AddrReq) (*pb.AddrResp, error) {
    return &pb.AddrResp{}, nil
}

func (ms *MetaServer) Partitions(context.Context, *pb.PartitionsReq) (*pb.PartitionsResp, error) {
    return &pb.PartitionsResp{}, nil
}

func (ms *MetaServer) PartitionStatus(context.Context, *pb.PartitionStatusReq) (*pb.PartitionStatusResp, error) {
    return &pb.PartitionStatusResp{}, nil
}

func (ms *MetaServer) ReConfigure(ctx context.Context, req *pb.ReConfigureReq) (*pb.ReConfigureResp, error) {
    glg.Infof("receive reconfig request from: %s, [%d, %v]", req.Lease.Sid, req.PartitionId, req.Vote)
    return &pb.ReConfigureResp{}, nil
}
